import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oss_upload/oss_upload.dart';
import 'package:richedit/data/doc_entity.dart';
import 'package:richedit/loading_widget.dart';
import 'package:richedit/widget/ppt_widget.dart';
import 'package:webview_flutter/webview_flutter.dart';

class DocEdit extends StatefulWidget {
  DocEntity data;

  DocEdit(this.data);

  @override
  _DocEditState createState() => _DocEditState();
}

class _DocEditState extends State<DocEdit> {
  int _uploadCount = 0;
  int _uploadTotal = 0;
  bool _isUpload = false;
  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () async{
          LoadingWidget.showLoading(context, text: "正在读取文件");
          FilePickerResult? result = await FilePicker.platform.pickFiles(
            type: FileType.custom,
            allowedExtensions: [],
          );

          LoadingWidget.dismissLoading();
          if (result == null){
            return;
          }
          var uploadFile = await OSSUpload.uploadFile(result.files.single.path!, "1", "1", "1", onSendProgress: (int count, int total){
            _uploadCount = count;
            _uploadTotal = total;
            _isUpload = true;
            setState(() {
            });
          });
          _isUpload = false;
          if (uploadFile != null){
            String fileUrl = uploadFile['url'];
            widget.data.url = fileUrl;
            setState(() {
            });
          }
        },
        child: Container(
          child: widget.data.url == null ? Container(
            width: 80,
            height: 80,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.black12, width: 1),
            ),
            child: _isUpload ? Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text("${(_uploadCount * 100 / _uploadTotal).toStringAsFixed(1)}%"),
                Padding(padding: const EdgeInsets.all(4)),
                Text("正在上传"),
              ],
            ) : Icon(Icons.file_copy_outlined),
          ) : Container(
            padding: const EdgeInsets.all(1),
            constraints: BoxConstraints(
              maxHeight: 300,
            ),
            child: DocShow(widget.data.url??""),
          ),
        ),
      ),
    );
  }
}

class DocPreview extends StatefulWidget {
  DocEntity data;

  DocPreview(this.data);

  @override
  _DocPreviewState createState() => _DocPreviewState();
}

class _DocPreviewState extends State<DocPreview> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(1),
      constraints: BoxConstraints(
        maxHeight: 300,
      ),
      child: DocShow(widget.data.url??""),
    );
  }
}

class DocShow extends StatefulWidget {
  String docUrl;

  DocShow(this.docUrl);

  @override
  _DocShowState createState() => _DocShowState();
}

class _DocShowState extends State<DocShow> {

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.docUrl.endsWith("ppt") || widget.docUrl.endsWith("pptx")){
      return PPTWidget(pptFilePath: widget.docUrl,);
    }
    return WebView(
      initialUrl: "https://ow365.cn/?i=19953&ssl=1&furl=${widget.docUrl}",
      onWebViewCreated: (controller){
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}


